﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using DataService;
using DTO;

public partial class Billing_EditBillingCard : BasePage
{
    private ContactService contactService = new ContactService();
    private IList<CardType> CardTypes;
    private IList<AccountType> accountTypes;
    private IList<AccountCategory> accountCategory;
    private string PaymentType = "";
    private CreditCard CreditCard;
    private BankAccount bankAccount;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (UserLogin != null)
        {
            CardTypes = contactService.GetListCardType();
            accountTypes = contactService.GetListAccountType();
            accountCategory = contactService.GetListAccountCategory();            
            if (!IsPostBack)
            {
                PaymentType = Convert.ToString(Request.QueryString["Payment"]);
                if (!string.IsNullOrEmpty(PaymentType))
                {
                    switch (PaymentType)
                    {
                        case "C":
                            SetControlEnable(true);
                            CreditCard = contactService.GetCreditCardByAccountId(UserLogin.Customer.Id);
                            if (CreditCard != null)
                            {
                                SetCreditCard(CreditCard);
                            }
                            break;
                        case "B":
                            SetControlEnable(false);
                            bankAccount = contactService.GetBankAccountInfo(UserLogin.Customer.Id);
                            if (bankAccount != null)
                            {
                                SetBankAccount(bankAccount);
                            }
                            break;
                        default:
                            break;
                    }
                }
                Initial();
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }
    }

    public void Initial()
    {
        if (CardTypes != null)
        {
            ddlTypeCard.DataSource = CardTypes;
            ddlTypeCard.DataTextField = "Name";
            ddlTypeCard.DataValueField = "Id";
        }
        if (accountTypes != null)
        {
            ddlAccountType.DataSource = accountTypes;
            ddlAccountType.DataMember = "Id";
            ddlAccountType.DataValueField = "Name";
        }
        if (accountCategory != null)
        {
            ddlBankAccountCategory.DataSource = accountCategory;
            ddlBankAccountCategory.DataMember = "Id";
            ddlBankAccountCategory.DataValueField = "Name";
        }
        DateTimeFormatInfo dfi = new DateTimeFormatInfo();
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem() { Text = i.ToString("00") + " - " + dfi.GetMonthName(i), Value = i.ToString() });
        }
        for (int i = 2009; i <= 2028; i++)
        {
            ddlYear.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
        }
        DataBind();
    }

    protected void radCard_CheckedChanged(object sender, EventArgs e)
    {
        if (radCard.Checked)
        {
            radBank.Checked = false;
            CreditCard = contactService.GetCreditCardByAccountId(UserLogin.Customer.Id);
            SetCreditCard(CreditCard);
            SetControlEnable(true);
        }
    }

    public void SetCreditCard(CreditCard card)
    {
        if (card != null)
        {
            txtHolder.Text = card.CardHolder;
            txtNumber.Text = card.Number;
            if (card.Type != null)
            {
                ddlTypeCard.Text = card.Type.Id.ToString();
            }
            ddlMonth.SelectedIndex = card.ExpMonth - 1;
            ddlYear.Text = card.ExpYear.ToString();
        }
    }

    public void SetBankAccount(BankAccount bank)
    {
        if (bank != null)
        {
            txtRountingNumber.Text = bank.RountingNumber;
            txtAccountNumber.Text = bank.AccountNumber;
            if (bank.AccountCategory != null)
            {
                ddlBankAccountCategory.Text = bank.AccountCategory.Name;
            }
            if (bank.AccountType != null)
            {
                ddlAccountType.Text = bank.AccountType.Name;
            }
        }
    }

    private void SetControlEnable(bool value)
    {
        radCard.Checked = value;
        txtHolder.Enabled = value;
        txtNumber.Enabled = value;
        ddlTypeCard.Enabled = value;
        ddlMonth.Enabled = value;
        ddlYear.Enabled = value;

        radBank.Checked = !value;
        txtRountingNumber.Enabled = !value;
        txtAccountNumber.Enabled = !value;
        ddlAccountType.Enabled = !value;
        ddlBankAccountCategory.Enabled = !value;
    }

    protected void radBank_CheckedChanged(object sender, EventArgs e)
    {
        if (radBank.Checked)
        {
            radCard.Checked = false;
            bankAccount = contactService.GetBankAccountInfo(UserLogin.Customer.Id);
            SetBankAccount(bankAccount);
            SetControlEnable(false);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (UserLogin != null)
        {
            if (radCard.Checked)
            {
                CreditCard BillingCard = new CreditCard();
                BillingCard.CardHolder = txtHolder.Text.Trim();
                BillingCard.Number = txtNumber.Text.Trim();
                if (CardTypes != null)
                    BillingCard.Type = CardTypes[ddlTypeCard.SelectedIndex];
                BillingCard.ExpMonth = Convert.ToInt32(ddlMonth.SelectedValue);
                BillingCard.ExpYear = Convert.ToInt32(ddlYear.SelectedValue);
                contactService.InsertUpdateCardAccount(UserLogin.Customer.Id, BillingCard);
            }
            else
            {
                BankAccount BankAccount = new BankAccount();
                if (ddlBankAccountCategory.SelectedIndex >= 0 && accountCategory != null)
                    BankAccount.AccountCategory = accountCategory[ddlBankAccountCategory.SelectedIndex];
                if (ddlAccountType.SelectedIndex >= 0 && accountTypes != null)
                    BankAccount.AccountType = accountTypes[ddlAccountType.SelectedIndex];
                BankAccount.AccountNumber = txtAccountNumber.Text;
                BankAccount.RountingNumber = txtRountingNumber.Text;
                contactService.InsertUpdateBankAccount(UserLogin.Customer.Id, BankAccount);
            }
            Response.Redirect("~/Billing/default.aspx");
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }
            
    }
}